package dotsandboxs;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

public class StartMenu {
    /**
     * 人-人 模式
     */
    final int MAN_MAN_MODE = 1;
    /**
     * 人-电脑 模式
     */
    final int MAN_MACHINE_MODE = 2;
    /**
     * 电脑-电脑 模式
     */
    final int MACHINE_MACHINE_MODE = 3;
    ChessGUI gui;

    public StartMenu(ChessGUI gui) {
        this.gui = gui;
        JDialog dialog = new JDialog(gui.frame, "选择模式", true);
        dialog.setSize(300, 250);
        JLabel label = new JLabel("          请选择游戏模式，默认为“人vs人”模式");
        JButton button1 = new JButton("确定");
        JButton button2 = new JButton("取消");
        JPanel panel1 = new JPanel();
        JRadioButton radioButton1 = new JRadioButton("人vs人", true);
        JRadioButton radioButton2 = new JRadioButton("人vs电脑");
        JRadioButton radioButton3 = new JRadioButton("电脑vs电脑");
        ButtonGroup group = new ButtonGroup();
        group.add(radioButton1);
        group.add(radioButton2);
        group.add(radioButton3);
        dialog.setLayout(new GridLayout(5, 1, 0, 10));
        dialog.add(label);
        dialog.add(radioButton1);
        dialog.add(radioButton2);
        dialog.add(radioButton3);
        dialog.add(panel1);
        panel1.add(button1);
        panel1.add(button2);
        button1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                // 人vs人模式
                if (radioButton1.isSelected()) {
                    dialog.dispose();
                    JDialog dialog = new JDialog(gui.frame, "选择先行方", true);
                    JLabel label = new JLabel("请选择先行棋方，默认为红方先行");
                    JButton button1 = new JButton("开始");
                    JButton button2 = new JButton("取消");
                    JPanel panel1 = new JPanel();
                    JPanel panel2 = new JPanel();
                    JRadioButton radioButton1 = new JRadioButton("红方", true);
                    JRadioButton radioButton2 = new JRadioButton("蓝方");
                    ButtonGroup group = new ButtonGroup();
                    group.add(radioButton1);
                    group.add(radioButton2);
                    dialog.setSize(200, 150);
                    dialog.setLayout(new GridLayout(3, 1));
                    dialog.add(label);
                    dialog.add(panel1);
                    dialog.add(panel2);
                    panel1.add(radioButton1);
                    panel1.add(radioButton2);
                    panel2.add(button1);
                    panel2.add(button2);
                    button1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if (radioButton1.isSelected()) {
                                ChessGUI.color += 2;// 行棋方计数器+2
                            }
                            if (radioButton2.isSelected()) {
                                ChessGUI.color++;// 行棋方计数器+1
                            }
                            ChessGUI.gameMode = MAN_MAN_MODE;// 标志游戏的开始
                            dialog.dispose();
                        }
                    });
                    button2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                        }
                    });
                    dialog.setLocationRelativeTo(null);
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                }
                // 人vs电脑模式
                if (radioButton2.isSelected()) {
                    dialog.dispose();
                    JDialog dialog = new JDialog(gui.frame, "选择先行方", true);
                    JLabel label = new JLabel("请选择己方棋种，默认为红方");
                    JButton button1 = new JButton("开始");
                    JButton button2 = new JButton("取消");
                    JPanel panel1 = new JPanel();
                    JPanel panel2 = new JPanel();
                    JRadioButton radioButton1 = new JRadioButton("红方", true);
                    JRadioButton radioButton2 = new JRadioButton("蓝方");
                    ButtonGroup group = new ButtonGroup();
                    group.add(radioButton1);
                    group.add(radioButton2);
                    dialog.setSize(200, 150);
                    dialog.setLayout(new GridLayout(3, 1));
                    dialog.add(label);
                    dialog.add(panel1);
                    dialog.add(panel2);
                    panel1.add(radioButton1);
                    panel1.add(radioButton2);
                    panel2.add(button1);
                    panel2.add(button2);
                    button1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if (radioButton1.isSelected()) {
                                ChessGUI.color += 2;// 行棋方计数器+2
                            }
                            if (radioButton2.isSelected()) {
                                ChessGUI.color++;// 行棋方计数器+1
                            }
                            ChessGUI.gameMode = MAN_MACHINE_MODE;// 标志游戏的开始
                            dialog.dispose();
                        }
                    });
                    button2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                        }
                    });
                    dialog.setLocationRelativeTo(null);
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                }
                // 电脑vs电脑模式
                if (radioButton3.isSelected()) {
                    dialog.dispose();
                    JDialog dialog = new JDialog(gui.frame, "模式配置", true);
                    JLabel label = new JLabel("请选择先行棋方，默认为红方先行");
                    JButton button1 = new JButton("开始");
                    JButton button2 = new JButton("取消");
                    JPanel panel1 = new JPanel();
                    JPanel panel2 = new JPanel();
                    JRadioButton radioButton1 = new JRadioButton("红方", true);
                    JRadioButton radioButton2 = new JRadioButton("蓝方");
                    ButtonGroup group = new ButtonGroup();
                    group.add(radioButton1);
                    group.add(radioButton2);
                    dialog.setSize(200, 150);
                    dialog.setLayout(new GridLayout(3, 1));
                    dialog.add(label);
                    dialog.add(panel1);
                    dialog.add(panel2);
                    panel1.add(radioButton1);
                    panel1.add(radioButton2);
                    panel2.add(button1);
                    panel2.add(button2);
                    button1.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            if (radioButton1.isSelected()) {
                                ChessGUI.color += 2;// 行棋方计数器+2
                            }

                            if (radioButton2.isSelected()) {
                                ChessGUI.color++;// 行棋方计数器+1
                            }
                            ChessGUI.gameMode = MACHINE_MACHINE_MODE;// 标志游戏的开始
                            dialog.dispose();
                        }
                    });
                    button2.addActionListener(new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            dialog.dispose();
                        }
                    });
                    dialog.setLocationRelativeTo(null);
                    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                    dialog.setVisible(true);
                }
            }
        });
        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.dispose();
            }
        });
        dialog.setLocationRelativeTo(null);
        dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        dialog.setVisible(true);
    }
}
